package com.javandad.app.client.project;

import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.javandad.app.shared.entity.project.Post;
import com.javandad.app.shared.entity.project.ProjectAssoc;

public class WallTab extends AbsolutePanel {

	/**
	 * @wbp.parser.constructor
	 */
	public WallTab(ProjectServiceAsync projectService, final String projectName, final String username, boolean owner) {
		this.projectService = projectService;
		this.projectName = projectName;
		this.username = username;
		this.owner = owner;

		this.setSize("914px", "800px");
		cellList = new CellList<Post>(new AbstractCell<Post>() {
			@Override
			public void render(Context context, Post value, SafeHtmlBuilder sb) {
				// TODO
				sb.appendHtmlConstant("<p><table align='left' border='0' bgcolor='white' ><tr aligh='left'><td><table><tr ><td><img width='64' height='64' src='" + value.getUser().getProfileFodoURL()
						+ "'></img></td></tr></table></td>");
				sb.appendHtmlConstant("<td width='100%' valign='top' align='left'><table width='100%' height='100%'><tr valign='top' height='10%'><td width='100%'><b>");
				sb.appendEscaped(value.getUsername());
				sb.appendHtmlConstant("</b></td></tr>");
				sb.appendHtmlConstant("<tr valign='top' width='100%' height='85%'><td>");

				sb.appendEscaped(value.getText());
				sb.appendHtmlConstant("</td></tr><tr height='5%' valign='bottom'><td width='100%'>");
				sb.appendEscaped("posted at " + value.getDate());
				sb.appendHtmlConstant("</td></tr></table></td></tr></table></p>");

			}
		});

		cellList.setTitle("Posts");
		add(cellList, 10, 162);
		cellList.setSize("894px", "466px");

		add(textArea, 10, 10);
		textArea.setSize("884px", "71px");
		comboBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				showPosts();
			}
		});

		add(comboBox, 10, 133);
		comboBox.setSize("272px", "23px");
		comboBox.addItem("All");
		comboBox.addItem("All except project updates");
		comboBox.addItem("Project Updates");
		comboBox.addItem("Posts from project followers");
		comboBox.addItem("Posts from project members");
		comboBox.addItem("Posts from project owners");
		comboBox.addItem("Posts from project owners and members");

		Button btnPost = new Button("Post");
		btnPost.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Post post = new Post(projectName, username, "Post", textArea.getText(), null);
				addPost(post);
				showPosts();

			}
		});
		add(btnPost, 839, 130);
		btnPost.setSize("65px", "26px");
	}

	@Override
	protected void onLoad() {
		super.onLoad();

		showPosts();

	}

	private void showPosts() {
		int level = ProjectAssoc.NOASSOC;
		int index = comboBox.getSelectedIndex();
		switch (index) {
		case 0:
			level = ProjectAssoc.NOASSOC | ProjectAssoc.FOLLOWING_PROJECT | ProjectAssoc.PARTICIPATING_PROJECT | ProjectAssoc.OWNING_PROJECT | 16;
			break;
		case 1:
			level = ProjectAssoc.NOASSOC | ProjectAssoc.FOLLOWING_PROJECT | ProjectAssoc.PARTICIPATING_PROJECT | ProjectAssoc.OWNING_PROJECT;
			break;
		case 2:
			level = 16;
			break;
		case 3:
			level = ProjectAssoc.FOLLOWING_PROJECT;
			break;
		case 4:
			level = ProjectAssoc.PARTICIPATING_PROJECT;
			break;
		case 5:
			level = ProjectAssoc.OWNING_PROJECT;
			break;
		case 6:
			level = ProjectAssoc.PARTICIPATING_PROJECT | ProjectAssoc.OWNING_PROJECT;
			break;

		}

		projectService.downloadPosts(projectName, username, level, NO_POSTS_SHOWN, new AsyncCallback<List<Post>>() {

			@Override
			public void onSuccess(List<Post> result) {
				cellList.setRowData(result);

			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});

	}

	private void addPost(Post post) {
		projectService.uploadPost(post, new AsyncCallback<Post>() {

			@Override
			public void onSuccess(Post result) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	private String projectName = "";
	private String username = "";
	@SuppressWarnings("unused")
	private boolean owner = false;

	private final int NO_POSTS_SHOWN = 10;

	private CellList<Post> cellList;

	private ProjectServiceAsync projectService = null;

	private final TextArea textArea = new TextArea();
	private ListBox comboBox = new ListBox();
}
